<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()" ref="dataForm">
      <el-form-item>
        <el-select v-model="dataForm.isMember" placeholder="请选择会员状态">
          <el-option v-for="item in options" :key="item.val" :label="item.label" :value="item.val"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.title" placeholder="职位">
          <el-option
            v-for="item in position"
            :key="item.val"
            :label="item.label"
            :value="item.val"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.userId" placeholder="uid"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.nickname" placeholder="昵称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="search()">查询</el-button>
        <el-button type="primary" @click="download()">下载数据</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;"
    >
      <el-table-column prop="userId" header-align="center" align="center" label="uid"></el-table-column>
      <el-table-column prop="nickname" header-align="center" align="center" label="昵称"></el-table-column>
      <el-table-column header-align="center" align="center" label="头像">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" style="width:40px;height:40px;">
        </template>
      </el-table-column>
      <el-table-column prop="phone" header-align="center" align="center" label="手机号"></el-table-column>
      <el-table-column prop="wxNumber" header-align="center" align="center" label="微信号"></el-table-column>
      <el-table-column prop="wxQrcode" header-align="center" align="center" label="二维码">
        <template slot-scope="scope">
          <img v-if="scope.row.wxQrcode" :src="scope.row.codeImg" style="width:40px;height:40px;">
          <span v-else></span>
        </template>
      </el-table-column>
      <el-table-column prop="gmtCreate" header-align="center" align="center" label="注册时间"></el-table-column>
      <el-table-column prop="memberStartTime" header-align="center" align="center" label="会员购买时间"></el-table-column>
      <el-table-column header-align="center" align="center" label="会员状态">
        <template slot-scope="scope">
          <span>{{ scope.row.memberStatus === 0 ? '过期' : '未过期' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="memberEndTime" header-align="center" align="center" label="会员到期日"></el-table-column>
      <el-table-column prop="gmtModified" header-align="center" align="center" label="成为经理时间"></el-table-column>
      <el-table-column header-align="center" align="center" label="职位">
        <template slot-scope="scope">
          <span>{{ scope.row.title === 0 ? '班长(正常分销) ' : '' }}</span>
          <span>{{ scope.row.title === 1 ? '班主任' : '' }}</span>
          <span>{{ scope.row.title === 2 ? '班长(禁止分销)' : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="stewardshipNickName" header-align="center" align="center" label="管家昵称"></el-table-column>
      <el-table-column prop="totalCredit" header-align="center" align="center" label="学分"></el-table-column>
      <el-table-column prop="totalLove" header-align="center" align="center" label="爱心值"></el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
  </div>
</template>

<script>
import QRCode from "qrcode";
export default {
  data() {
    return {
      dataForm: {
        phone: "",
        isMember: "",
        grade: "",
        userId: "",
        nickname: "",
        title:'',
      },
      // 会员状态
      options: [
        {
          val: null,
          label: "全部"
        },
        {
          val: "0",
          label: "过期"
        },
        {
          val: "1",
          label: "未过期"
        }
      ],
      //职位
      position: [
        {
          val: null,
          label: "全部"
        },
        {
          val: "0",
          label: "班长(正常分销)"
        },
        {
          val: "1",
          label: "班主任"
        },
        {
          val: "2",
          label: "班长(禁止分销)"
        },
      ],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    };
  },
  activated() {
    this.getDataList();
  },
  methods: {
    search() {
      this.pageIndex = 1
      this.getDataList()
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let obj = {
        page: this.pageIndex,
        limit: this.pageSize,
        key: this.dataForm.key,
        grade: 2
      };
      if (this.dataForm.isMember) {
        obj.isMember = this.dataForm.isMember;
      }
      if (this.dataForm.userId) {
        obj.userId = this.dataForm.userId + "";
      }
      if (this.dataForm.nickname) {
        obj.nickname = this.dataForm.nickname;
      }
      if (this.dataForm.phone) {
        obj.phone = this.dataForm.phone;
      }
      if(this.dataForm.title){
        obj.title = this.dataForm.title;
      }
      this.$http({
        url: this.$http.adornUrl("/user/appuser/overMemberList"),
        method: "get",
        params: this.$http.adornParams(obj)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
          let len = data.page.list.length;
          for (let i = 0; i < len; i++) {
            let codeUrl = this.dataList[i].wxQrcode || "";
            let opts = {
              errorCorrectionLevel: "H",
              type: "image/jpeg",
              rendererOpts: {
                quality: 0.3
              }
            };
            QRCode.toDataURL(codeUrl, opts, (err, url) => {
              this.dataList[i].codeImg = url;
            });
          }
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    download() {
      let obj = {
        page: this.pageIndex,
        limit: this.pageSize,
        key: this.dataForm.key,
        grade: 2
      };
      if (this.dataForm.isMember) {
        obj.isMember = this.dataForm.isMember;
      }
      if (this.dataForm.userId) {
        obj.userId = this.dataForm.userId + "";
      }
      if (this.dataForm.nickname) {
        obj.nickname = this.dataForm.nickname;
      }
      if (this.dataForm.phone) {
        obj.phone = this.dataForm.phone;
      }
      if(this.dataForm.title){
        obj.title = this.dataForm.title;
      }
      this.$http({
        url: this.$http.adornUrl("/user/appuser/overMemberList/download"),
        method: "get",
        params: this.$http.adornParams(obj),
        headers: {
          "Content-Type": "application/json; application/octet-stream"
        },
        responseType: "arraybuffer"
      }).then(({ data }) => {
        const blob = new Blob([data], { type: "application/vnd.ms-excel" });
        const downloadElement = document.createElement("a");
        const href = window.URL.createObjectURL(blob);
        downloadElement.href = href;
        downloadElement.download = "经理列表.xls";
        document.body.appendChild(downloadElement);
        downloadElement.click();
        document.body.removeChild(downloadElement); // 下载完成移除元素
        window.URL.revokeObjectURL(href); // 释放掉blob对象
      });
    }
  }
};
</script>
